package hc.bre.deci.tree

import static org.assertj.core.api.InstanceOfAssertFactories.STRING

import hc.bre.cmn.ListOption
import hc.bre.cmn.RuleDefinition
import hc.bre.cmn.RuleScriptBuilder
import hc.bre.data.encoder.EncoderDefinition
import hc.bre.data.encoder.EncoderItem
import hc.bre.deci.action.ActionBuilder
import hc.bre.deci.action.ActionDefinition

@groovy.transform.CompileStatic
class TreeBuilder extends RuleScriptBuilder<TreeDefinition>{

	@Override
	public void buildClassRuleBodyCore() {
		buildBranch(ruleDef.root);
	}

	public void buildBranch(TreeBranchItem item) {
		if(item.rpath == null || item.rpath =='') {
			buildActionList(item);
			return;
		};

		String apply = referApplyRule(item.rpath)//referApplyRule(item.rid);

		apdNl("switch(${apply}.clas.id){");

		nested{
			item.list.each { TreeBranchItem caseBranch ->
				apdNl("case ${caseBranch.referId}:");

				indentInc();

				buildBranch(caseBranch);

				apdNl("break;")

				indentDec();
			}

			apdNl("default : assert false;break;")
		}

		apdNl("};")
	}

	public void buildActionList(TreeBranchItem item) {

		apdNl("${result}.branchId = ${item.id};");

		int count = ruleDef.actionDefList.size();

		apdNl("def ${rresult};")

		for(int i = 0 ; i < count; i++) {
			ActionDefinition act = ruleDef.actionDefList[i];

			ActionBuilder.build(act, item.valueMap[act.code], this);
		}
	}
}
